layui.use(['table','layer','form','laydate'], function() {
	var table = layui.table;
	var layer = layui.layer;
	var form = layui.form;
	var laydate = layui.laydate;
	var $ = layui.jquery;
	
	//新增弹窗中的保存添加事件
		$("body").on("click","#saveBtn",function(){	//二次选择
			//获取form的全部数据
			var data = form.val("storeForm");
			//console.log(data);
			//把数据推送道后台
			//ajax
			$.post("/store/save",data,function(){
				console.log(data);
				layer.closeAll();
				$("#searchBtn").click();
			});
		});
	
	
	//给新增按钮绑定事件
	$("#addBtn").on("click",function(){		
		//弹出窗口
		$.get("/shop/top",function(tops){
		layer.open({
			type:1
			,area:['400px','500px']//窗口大小
			,title:"新增仓库"
			,content:$("#addStoreTemplate").html()
		});
		$.each(tops,function(index,value){
			$("#parentId").append("<option value='"+value.id+"'>"+value.name+"</option>")
		});
		
		form.render();
		});
		/*laydate.render({
			elem:'#birthday'//指定元素
		});*/
		
		//新增弹窗中的保存添加事件
		/*$("#saveBtn").on("click",function(){	
			//获取form的全部数据
			var data = form.val("storeForm");
			//console.log(data);
			//把数据推送道后台
			//ajax
			$.post("/store/save",data,function(){
				layer.closeAll();
				$("#searchBtn").click();
			});
		});*/
		
	});
	
	
	
	//给查询按钮绑定事件
	$("#searchBtn").on("click",function(){
		table.render({
			elem: '#storeTable'
			, url: '/store/list' //数据接口
			, page: true //开启分页
			, cols: [[ //表头
				{ field: 'id', title: 'ID', width: '10%', fixed: 'left' }
				, { field: 'name', title: '仓库名', width: '15%' }
				, { field: 'address', title: '地址', width: '20%'}
				, { field: 'admin', title: '管理员', width: '10%'}
				, { field: 'admin_tel', title: '管理员TEL', width: '10%' }
				, { field: 'shopname', title: '所属商店', width: '10%' }
				, { title: '操作', width: '25%' ,toolbar:"#operateTemplate"}
			]],
			limits:[5,10,15,20],
			limit:5,
			where:{"keyword":$("#searchStorename").val()}
		});
		
		table.on('tool(storeTable1)',function(obj){
			
			var storeid=obj.data.id;//获取当前操作行的id
			
			if(obj.event == "del" ){//点击删除按钮
				layer.confirm("你确定要删除该仓库么？",function(){
					$.post("/store/del",{id:storeid},function(){
						layer.closeAll();
						$("#searchBtn").click();
					});
				}/*,function(){}*/);
			}else if(obj.event == "edit"){//点击编辑按钮
			$.get("/shop/top",function(tops){
				$.get("/store/"+storeid,function(u){
					console.log(u.shop_id);
					layer.open({
						type:1
						,area:['400px','500px']//窗口大小
						,title:"编辑仓库"
						,content:$("#addStoreTemplate").html()
					});
					form.val("storeForm",u);//第一个获取值，第二个赋值给u
					
					$.each(tops,function(index,value){	
					
					$("#parentId").append("<option value='"+value.id+"'   "+(value.id==u.shop_id?'selected':'')+">"+value.name+"</option>")
					});
					form.render();
						});
				});
			}else{
				/*$.get("/storeProduct/index",{storeId:storeid},function(){*/
						window.location.href="/storeProduct/index?storeId="+storeid;
						
				/*});*/
				
			}
			
		});
		
	}).click();
	
});